
package com.v2future.sjt.collegecheckingattendance.message.adapter;

import java.util.List;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout.LayoutParams;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.v2future.sjt.collegecheckingattendance.MainActivity;
import com.v2future.sjt.collegecheckingattendance.R;
import com.v2future.sjt.collegecheckingattendance.message.bean.MessageBean;
import com.v2future.sjt.collegecheckingattendance.utils.WidgetUtil;


public class SwipeAdapter extends BaseAdapter {
    /**
     * 上下文对象
     */
    private Context mContext = null;
    private List<MessageBean> data;
    
    private int mRightWidth = 0;

    private onStatusClickListener statusClickListener;

    /**
     * @param
     */
    public SwipeAdapter(Context ctx, List<MessageBean> data, int rightWidth) {
        mContext = ctx;
        this.data = data;
        mRightWidth = rightWidth;
    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int position) {
        return data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    public void setStatusClickListener(onStatusClickListener statusClickListener) {
        this.statusClickListener = statusClickListener;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
    	
        ViewHolder holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.list_item, parent, false);
            holder = new ViewHolder();
            holder.item_left = (RelativeLayout)convertView.findViewById(R.id.item_left);
            
            holder.iv_icon = (ImageView) convertView.findViewById(R.id.iv_icon);
            holder.tv_leave_time = (TextView)convertView.findViewById(R.id.tv_title_text);
            holder.tv_msg = (TextView)convertView.findViewById(R.id.tv_msg);
            holder.tv_time = (TextView)convertView.findViewById(R.id.tv_time);
            holder.tv_status = (TextView)convertView.findViewById(R.id.tv_status);
            holder.tv_course = (TextView)convertView.findViewById(R.id.tv_course);
            holder.tv_course_name = (TextView)convertView.findViewById(R.id.tv_course_name);
            holder.tv_name = (TextView)convertView.findViewById(R.id.tv_name);
            
//            holder.item_right_txt = (TextView)convertView.findViewById(R.id.item_right_txt);
            convertView.setTag(holder);
        } else {// 有直接获得ViewHolder
            holder = (ViewHolder)convertView.getTag();
        }
        
//        LayoutParams lp1 = new LayoutParams(LayoutParams.MATCH_PARENT,
//                LayoutParams.MATCH_PARENT);
//        holder.item_left.setLayoutParams(lp1);
//        LayoutParams lp2 = new LayoutParams(mRightWidth, LayoutParams.MATCH_PARENT);
//        holder.item_right.setLayoutParams(lp2);

        MessageBean msg = data.get(position);
        WidgetUtil.setText(holder.tv_msg, msg.getActionDesc());
        WidgetUtil.setText(holder.tv_leave_time, msg.getLeaveDate());
        WidgetUtil.setText(holder.tv_course_name, msg.getCourseName());
//        WidgetUtil.setText(holder.tv_course, "第"+msg.getCourseNumber()+"节课");
        holder.tv_course.setVisibility(View.GONE);
        WidgetUtil.setText(holder.tv_time, "申请时间："+msg.getSubmitDate());
        WidgetUtil.setText(holder.tv_name, msg.getUserName());
        setLeaveStatus(holder.tv_status, msg.getApproveCode(), msg);
        if (!TextUtils.isEmpty(msg.getPortraitNum())) {
            try {
                int headNumber = Integer.parseInt(msg.getPortraitNum());
                int headId = ((MainActivity) mContext).getHeadIconId(headNumber);
                holder.iv_icon.setImageResource(headId);
            }
            catch (Exception e){
                e.printStackTrace();
            }
        }
//        holder.iv_icon.setImageResource(msg.getIcon_id());
        
//        holder.item_right.setOnClickListener(new OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                if (mListener != null) {
//                    mListener.onRightItemClick(v, position);
//                }
//            }
//        });
        return convertView;
    }

    static class ViewHolder {
    	RelativeLayout item_left;
    	RelativeLayout item_right;

        TextView tv_msg;
        TextView tv_time;
        TextView tv_status;
        TextView tv_course;
        TextView tv_leave_time;
        TextView tv_course_name;
        TextView tv_name;
        ImageView iv_icon;

        TextView item_right_txt;
    }
    
    /**
     * 单击事件监听器
     */
    private onRightItemClickListener mListener = null;
    
    public void setOnRightItemClickListener(onRightItemClickListener listener){
    	mListener = listener;
    }

    public interface onRightItemClickListener {
        void onRightItemClick(View v, int position);
    }

    public interface onStatusClickListener {
        void onStatusClick(String pKApproHistoryId);
    }

    /**
     * 设置当前请假状态
     * @param tv
     * @param status
     */
    public void setLeaveStatus(TextView tv, String status, final MessageBean bean){
        if ("1".equals(status)) {
            tv.setText("已同意");
            tv.setTextColor(mContext.getResources().getColor(R.color.white));
            tv.setBackgroundColor(mContext.getResources().getColor(R.color.gray_text));
            tv.setOnClickListener(null);
        }
        else if ("2".equals(status)) {
            tv.setText("不通过");
            tv.setTextColor(mContext.getResources().getColor(R.color.white));
            tv.setBackgroundColor(mContext.getResources().getColor(R.color.gray_text));
            tv.setOnClickListener(null);
        }
        else if ("3".equals(status)) {
            tv.setTextColor(mContext.getResources().getColor(R.color.white));
            String userType = ((MainActivity)mContext).getUserInfo().getData().getSourceTypeId();
            if ("1".equals(userType)) {//老师
                tv.setText("同意");
                tv.setBackgroundColor(mContext.getResources().getColor(R.color.colorful_light));
                tv.setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        statusClickListener.onStatusClick(bean.getPkApproHistoryId());
                    }
                });
            }
            else {
                tv.setText("待审批");
                tv.setBackgroundColor(mContext.getResources().getColor(R.color.gray_text));
                tv.setOnClickListener(null);
            }
        }
        else if ("4".equals(status)) {
            tv.setText("已撤销");
            tv.setTextColor(mContext.getResources().getColor(R.color.white));
            tv.setBackgroundColor(mContext.getResources().getColor(R.color.gray_text));
            tv.setOnClickListener(null);
        }
    }
}
